import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';

interface Message {
	role_no: string;
	pk_val: string;
	search_field_val: string;
	productid: string;
}

interface D {
	_id: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const systemid = '12';
	const time_info = await sys_date(action_id, session_id);
	// const data_time =
	const { exec, insert } = operate(action_id, session_id, systemid);
	const { exec: e, prepare: q } = query(action_id, session_id, systemid);
	// const productid = msg.productid;
	q('allot_object_type', ['_id', 'productid'], { productid: msg.productid, object_type_desc: { $like: '功能权限' } }, 200, 1, [], []);
	const [data] = await e<D>();
	if (!data.length) {
		return { ret: 'fail', msg: `该${msg.productid}下没有object_type_desc为功能权限的数据` };
	}
	// console.log(data)
	insert('pub_role_allot', {
		role_no: msg.role_no,
		pk_val: msg.pk_val,
		search_field_val: msg.search_field_val,
		productid: msg.productid,
		create_time: time_info,
		object_type_id: data[0]._id
	});
	const res = await exec();
	if (res.code === 1) {
		return { ret: 'success' };
	} else {
		return { ret: 'fail', msg: '保存失败' };
	}
}
